/**
 * Copyright (C) 2011 Denatics Development
 * 
 * Programmed by David Tiersch <dtiersch@denatics.com>
 * 
 * This file is part of B0mberman.
 * 
 * B0mberman is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * B0mberman is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with B0mberman. If not, see <http://www.gnu.org/licenses/>.
 */
package com.denatics.bomberman.gui;

import java.util.List;

import com.denatics.bomberman.Config;
import com.denatics.bomberman.engine.GameAPI;
import com.denatics.bomberman.entities.Entity;
import com.denatics.bomberman.sprites.Sprite;

/**
 * This Object renders the gamefield.
 * 
 * @author David Tiersch <dtiersch@denatics.com>
 */
public final class GameField extends Bitmap {

    /**
     * Size of the border (on both sides).
     */
    private static final int BORDERSIZE = 2;

    /**
     * Height of the Gamefield.
     */
    private final int width;

    /**
     * Width of the Gamefield.
     */
    private final int height;

    /**
     * Initializes the Object.
     */
    public GameField() {
        super((Config.FIELD_DIM[0] + BORDERSIZE) * Config.ENTITYSIZE
                * Config.SCALE, (Config.FIELD_DIM[1] + BORDERSIZE)
                * Config.ENTITYSIZE * Config.SCALE);
        this.width =
                (Config.FIELD_DIM[0] + BORDERSIZE) * Config.ENTITYSIZE
                        * Config.SCALE;
        this.height =
                (Config.FIELD_DIM[1] + BORDERSIZE) * Config.ENTITYSIZE
                        * Config.SCALE;
    }

    /**
     * Renders the gamefield of the game.
     * 
     * @param game
     *            game to be rendered
     */
    public void render(final GameAPI game) {

        for (int y = 0; y < this.height; y++) {
            for (int x = 0; x < this.width; x++) {
                this.getPixels()[x + y * this.width] = 0x000000;
            }
        }

        // paint underlying Entity
        for (int j = -1; j <= Config.FIELD_DIM[1]; j++) {
            for (int i = -1; i <= Config.FIELD_DIM[0]; i++) {
                Entity e = game.getEntity(i, j);
                if (e != null) {
                    draw(e, i + 1, j + 1);
                }
            }
        }

        // paint Sprites on top
        for (int j = -1; j <= Config.FIELD_DIM[1]; j++) {
            for (int i = -1; i <= Config.FIELD_DIM[0]; i++) {
                List<Sprite> s = game.getSprites(i, j);
                if (s != null && s.size() > 0) {
                    for (int sPos = 0; sPos < s.size(); sPos++) {
                        draw(s.get(sPos), i + 1, j + 1);
                    }
                }
            }
        }

    }
}
